package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.Interceptor.CheckLoginInterceptor;
import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UserParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        Boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    @GetMapping("/sendVerifyCode")
    public JsonResult sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    @PostMapping("/regist")
    public JsonResult regist(String nickname, String password, String rpassword, String phone, String verifyCode) {
      /*  try {
            userInfoService.regist(nickname, password, rpassword, phone, verifyCode);
        } catch (LogicException e) {
            e.printStackTrace();
            //自定义给用户的异常
            return JsonResult.error(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);
        } catch (Exception ex) {
            //系统异常
            ex.printStackTrace();
            return JsonResult.defaultError();
        }*/
        userInfoService.regist(nickname, password, rpassword, phone, verifyCode);

        return JsonResult.success();
    }
    //用户登录
    @PostMapping ("/login")
    public Object login(String username,String password) {
        //用户登录校验逻辑
        UserInfo user=userInfoService.login(username,password);
        //把用户信息添加到redis中
       String token =userInfoRedisService.setToken(user);
        Map<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);
        return JsonResult.success(map);
    }
    @RequiredLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request) {
        Map<String, CheckLoginInterceptor> beans = applicationContext.getBeansOfType(CheckLoginInterceptor.class);

        //获得请求头信息token
        String token = request.getHeader("token");
        //到redis里取出user对象
        UserInfo user=userInfoRedisService.getUserInfoByToken(token);
      return JsonResult.success(user);
    }

    @RequiredLogin
    @GetMapping("/strategies/favor")
    public Object favor(String userId) {
//从redis中 获取sid集合
        return JsonResult.success(strategyStatisVORedisService.findStrategyidsByuserId(userId));
    }


}
